package com.example.yichuangyun.mvvm.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityManageInfoBinding;
import com.example.yichuangyun.model.company.CompanyLabelModel;
import com.example.yichuangyun.model.company.ManageInfoDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.RiskInfoViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.InfoTabAdapter;
import com.example.yichuangyun.mvvm.ui.adapter.ManageInfoAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 经营信息
 */
@SuppressLint("NotifyDataSetChanged")
public class ManageInfoActivity extends BaseActivity<ActivityManageInfoBinding, RiskInfoViewModel> {

    private final List<CompanyLabelModel> tabData = new ArrayList<>();
    private InfoTabAdapter tabAdapter;
    private final List<ManageInfoDataModel> data = new ArrayList<>();
    private int mPos;
    private LinearLayoutManager manager;

    @Override
    protected RiskInfoViewModel bindModel() {
        return getViewModel(this, RiskInfoViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.jyxx));
        tabData.add(new CompanyLabelModel(getString(R.string.swpj)));
        tabData.add(new CompanyLabelModel(getString(R.string.zzzs)));
        tabData.add(new CompanyLabelModel(getString(R.string.xzxk)));
        tabData.add(new CompanyLabelModel(getString(R.string.ybnsr)));

        data.add(new ManageInfoDataModel(getString(R.string.swpj), 1, false));
        data.add(new ManageInfoDataModel(getString(R.string.zzzs), 1, false));
        data.add(new ManageInfoDataModel(getString(R.string.xzxk), 1, false));
        data.add(new ManageInfoDataModel(getString(R.string.ybnsr), 1, false));

        manager = new LinearLayoutManager(this);
        binding.jyxxRecyclerView.setLayoutManager(manager);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String eid = bundle.getString(Constant.eidKey);
            int position = bundle.getInt(Constant.positionKey);
            tabData.get(position).setSelect(true);
            data.get(position).setOpen(true);
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
            viewModel.getManageInfo(eid);
        }
    }

    @Override
    protected void initClick() {
        viewModel.manageInfoDataModelMutableLiveData.observe(this, manageInfoDataModel -> {
            // 税务评级
            if (null != manageInfoDataModel.getPayTaxes() && manageInfoDataModel.getPayTaxes().size() > 0) {
                data.get(0).setPayTaxes(manageInfoDataModel.getPayTaxes());
                tabData.get(0).setCount(manageInfoDataModel.getPayTaxes().size());
            }
            // 资质证书
            if (null != manageInfoDataModel.getCertificates() && manageInfoDataModel.getCertificates().size() > 0) {
                data.get(1).setCertificates(manageInfoDataModel.getCertificates());
                tabData.get(1).setCount(manageInfoDataModel.getCertificates().size());
            }
            // 行政许可
            if (null != manageInfoDataModel.getLicenseInfos() && manageInfoDataModel.getLicenseInfos().size() > 0) {
                data.get(2).setLicenseInfos(manageInfoDataModel.getLicenseInfos());
                tabData.get(2).setCount(manageInfoDataModel.getLicenseInfos().size());
            }
            // 一般纳税人
            if (null != manageInfoDataModel.getGeneralTaxpayers() && manageInfoDataModel.getGeneralTaxpayers().size() > 0) {
                data.get(3).setGeneralTaxpayers(manageInfoDataModel.getGeneralTaxpayers());
                tabData.get(3).setCount(manageInfoDataModel.getGeneralTaxpayers().size());
            }
            setTabAdapter();
        });

        binding.jyxxRecyclerView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
            int position = manager.findFirstVisibleItemPosition();
            if (position != mPos) {
                mPos = position;
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
                if (null != linearLayoutManager) {
                    linearLayoutManager.scrollToPositionWithOffset(position, 0);
                    for (CompanyLabelModel labelModel : tabData) {
                        labelModel.setSelect(false);
                    }
                    tabData.get(position).setSelect(true);
                    tabAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void setTabAdapter() {
        tabAdapter = new InfoTabAdapter(context, tabData);
        binding.recyclerView.setAdapter(tabAdapter);
        tabAdapter.setOnTabItemClickListener(position -> {
            for (CompanyLabelModel labelModel : tabData) {
                labelModel.setSelect(false);
            }
            tabData.get(position).setSelect(true);
            tabAdapter.notifyDataSetChanged();
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.jyxxRecyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
        });
        ManageInfoAdapter manageInfoAdapter = new ManageInfoAdapter(context, data);
        binding.jyxxRecyclerView.setAdapter(manageInfoAdapter);
    }
}